package com.gaber.springbootorderrabbitmqproducer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author gaber
 * @date 2023/9/18 18:19:02
 * @description TODO
 */
@Configuration
public class TTLRabbitMqConfiguration {
    @Bean
    public DirectExchange ttldirectExchange() {
        return new DirectExchange("ttl_direct_exchange", true, false);
    }

    @Bean
    public Queue ttldirectQueue() {
        // 设置过期时间
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 5000); // 这里一定是int类型
        args.put("x-max-length", 5); // 这里是int类型
        // 建立失信队列
        args.put("x-dead-letter-exchange", "dead_direct_exchange");
        args.put("x-dead-letter-routing-key", "dead"); // fanout不需要配置
        return new Queue("ttl.direct.queue", true, false, false, args);
    }


    @Bean
    public Binding ttldirectBingding() {
        return BindingBuilder.bind(ttldirectQueue()).to(ttldirectExchange()).with("ttl");
    }

    @Bean
    public Queue ttlMessagedirectQueue() {
        return new Queue("ttl.message.direct.queue", true);
    }


    @Bean
    public Binding ttlmsgdirectBingding() {
        return BindingBuilder.bind(ttlMessagedirectQueue()).to(ttldirectExchange()).with("ttlmessage");
    }
}
